home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Utilities / MView / gxu / gxcrackfvf.h < prev    next >
Encoding:
C/C++ Source or Header  |  2004-09-30  |  15.7 KB  |  498 lines

  1. #pragma once
  2.  
  3. #ifndef __DXCRACKFVF_H__
  4. #define __DXCRACKFVF_H__
  5.  
  6. /*//////////////////////////////////////////////////////////////////////////////
  7. //
  8. // File: DXCrackFVF.h
  9. //
  10. // Copyright (C) 1999 Microsoft Corporation. All Rights Reserved.
  11. //
  12. //
  13. //////////////////////////////////////////////////////////////////////////////*/
  14.  
  15.  
  16. // -------------------------------------------------------------------------------
  17. //  class DXCrackFVF
  18. //          used to simplify retreiving and storing data to FVF formats.
  19. //          on construction, it creates a set of offsets in the FVF format
  20. //          that it uses in the the methods to access the correct members
  21. //
  22. class DXCrackFVF
  23. {
  24. public:
  25.     inline DXCrackFVF(DWORD dwFVF);
  26.  
  27.     inline PBYTE GetArrayElem(PVOID pvPoints, DWORD iElem);
  28.  
  29.     inline void         SetPosition    (PVOID pvPoint, D3DXVECTOR3 *pvPos);
  30.     inline void         SetNormal      (PVOID pvPoint, D3DXVECTOR3 *pvNormal);
  31.     inline void         SetPointSize   (PVOID pvPoint, float fPointSize);
  32.     inline void         SetDiffuse     (PVOID pvPoint, D3DCOLOR color);
  33.     inline void         SetSpecular    (PVOID pvPoint, D3DCOLOR color);
  34.     inline void         SetTex1        (PVOID pvPoint, D3DXVECTOR2 *puvTex1);
  35.  
  36.     inline void         SetTexCoord    (PVOID pvPoint, UINT iTexCoord, D3DXVECTOR2 *puvTex1);
  37.     inline void         SetTexCoord    (PVOID pvPoint, UINT iTexCoord, D3DXVECTOR3 *puvTex1);
  38.     inline void         SetTexCoord    (PVOID pvPoint, UINT iTexCoord, D3DXVECTOR4 *puvTex1);
  39.     inline void         SetTexCoord    (PVOID pvPoint, UINT iTexCoord, float *);
  40.   
  41.     inline void         SetWeight      (PVOID pvPoint, UINT iWeight,   float fWeight);
  42.     inline void         SetIndices     (PVOID pvPoint, DWORD dwIndices);
  43.  
  44.  
  45.     inline D3DXVECTOR3 *PvGetPosition    (PVOID pvPoint);
  46.     inline D3DXVECTOR3 *PvGetNormal      (PVOID pvPoint);
  47.     inline float        FGetPointSize    (PVOID pvPoint);
  48.     inline D3DCOLOR     ColorGetDiffuse  (PVOID pvPoint);
  49.     inline D3DCOLOR     ColorGetSpecular (PVOID pvPoint);
  50.     inline D3DXVECTOR2 *PuvGetTex1       (PVOID pvPoint);
  51.     inline float        *PfGetWeights     (PVOID pvPoint);
  52.     inline DWORD        DwGetIndices     (PVOID pvPoint);
  53.  
  54.     inline float       *PxGetTexCoord   (PVOID pvPoint,UINT iTexCoord);
  55.     inline D3DXVECTOR2 *PxyGetTexCoord  (PVOID pvPoint,UINT iTexCoord);
  56.     inline D3DXVECTOR3 *PxyzGetTexCoord (PVOID pvPoint,UINT iTexCoord);
  57.     inline D3DXVECTOR4 *PxyzwGetTexCoord(PVOID pvPoint,UINT iTexCoord);
  58.  
  59.     inline float        FGetWeight     (PVOID pvPoint, UINT iWeight);
  60.  
  61.     inline bool         BPosition()     { return true; }
  62.     inline bool         BNormal()       { return m_oNormal > 0; }
  63.     inline bool         BPointSize()    { return m_oPointSize > 0; }
  64.     inline bool         BDiffuse()      { return m_oDiffuse > 0; }
  65.     inline bool         BSpecular()     { return m_oSpecular > 0; }
  66.     inline bool         BTex1()         { return m_oTex[0] > 0; }
  67.  
  68.     inline DWORD        CWeights()      { return m_cWeights; }
  69.     inline DWORD        CTexCoords()    { return m_cTexCoords; }
  70.     inline DWORD        CbTexCoordSize(DWORD iTexCoord);
  71.     inline void         GetTexCoordSizes(DWORD *rgdwTexCoordSizes);
  72.  
  73.     inline BOOL         BIndexedWeights() { return (m_dwFVF & D3DFVF_LASTBETA_UBYTE4|D3DFVF_LASTBETA_D3DCOLOR) && m_cWeights; }
  74.  
  75.  
  76.     DWORD m_dwFVF;
  77.     DWORD m_cBytesPerVertex; 
  78.     DWORD m_oNormal;
  79.     DWORD m_oPointSize;
  80.     DWORD m_oDiffuse;
  81.     DWORD m_oSpecular;
  82.     DWORD m_oTex[8];
  83.     DWORD m_cWeights;
  84.     DWORD m_cTexCoords;
  85. };
  86.  
  87.  
  88. // -------------------------------------------------------------------------------
  89. //  class DXCrackFVF inline functions - used to simplify access to the FVF 
  90. //
  91.  
  92. const DWORD x_rgdwTexCoordSize[] = { 2 * sizeof(float), 3 * sizeof(float), 4 * sizeof(float), sizeof(float)};
  93.  
  94. // -------------------------------------------------------------------------------
  95. //  constructor for DXCrackFVF, generates offsets used to access members
  96. //      of the given FVF
  97. //
  98. inline 
  99. DXCrackFVF::DXCrackFVF(DWORD dwFVF)
  100. :m_dwFVF(dwFVF),
  101. m_oNormal(0),
  102. m_oPointSize(0),
  103. m_oDiffuse(0),
  104. m_oSpecular(0),
  105. m_cWeights(0),
  106. m_cTexCoords(0)
  107. {
  108.     DWORD oCur = 0;
  109.     DWORD dwPosition;
  110.     DWORD dwTexCoordInfo;
  111.     DWORD iTexCoord;
  112.     DWORD dwTexCoordSize;
  113.     m_oTex[0] = 0;
  114.  
  115.     dwPosition = dwFVF & D3DFVF_POSITION_MASK;
  116.  
  117.     if (dwPosition == D3DFVF_XYZ)
  118.     {
  119.         oCur += 3 * sizeof(float);
  120.     }
  121.     else if (dwPosition == D3DFVF_XYZRHW)
  122.     {
  123.         oCur += 4 * sizeof(float);
  124.     }
  125.     else if (dwPosition >= D3DFVF_XYZB1)
  126.     {
  127.         m_cWeights = (dwPosition >> 1) - 2;
  128.         oCur += (3 + m_cWeights) * sizeof(float);
  129.     }
  130.  
  131.     if ( dwFVF & D3DFVF_NORMAL )
  132.     {
  133.         m_oNormal = oCur;
  134.         oCur += 3 * sizeof(float);
  135.     }
  136.  
  137.     if ( dwFVF & D3DFVF_PSIZE )
  138.     {
  139.         m_oPointSize = oCur;
  140.         oCur += sizeof(float);
  141.     }
  142.  
  143.     if ( dwFVF & D3DFVF_DIFFUSE )
  144.     {
  145.         m_oDiffuse = oCur;
  146.         oCur += sizeof(D3DCOLOR);
  147.     }
  148.  
  149.     if ( dwFVF & D3DFVF_SPECULAR )
  150.     {
  151.         m_oSpecular = oCur;
  152.         oCur += sizeof(D3DCOLOR);
  153.     }
  154.  
  155.     if ( dwFVF & D3DFVF_TEXCOUNT_MASK )
  156.     {
  157.         m_oTex[0] = oCur;
  158.  
  159.         // only one tu/tv set currently supported
  160.  
  161.         m_cTexCoords = ((dwFVF & D3DFVF_TEXCOUNT_MASK)>>D3DFVF_TEXCOUNT_SHIFT);
  162.  
  163.         if(m_cTexCoords > 8)
  164.             m_cTexCoords = 8;
  165.  
  166.         // all 2d, then go quick route
  167.         //if ((dwFVF & 0xffff0000) == 0)
  168.         //{
  169.             //oCur += sizeof(float) * 2 * m_cTexCoords;
  170.         //}
  171.         //else
  172.         {
  173.             dwTexCoordSize = (dwFVF & 0xffff0000) >> 16;
  174.             for (iTexCoord = 0; iTexCoord < m_cTexCoords; iTexCoord++)
  175.             {
  176.                 m_oTex[iTexCoord] = oCur;
  177.                 oCur += x_rgdwTexCoordSize[dwTexCoordSize & 0x3];
  178.                 dwTexCoordSize = dwTexCoordSize >> 2;
  179.             }
  180.         }
  181.     }
  182.  
  183.     m_cBytesPerVertex = oCur;
  184. }
  185.  
  186.  
  187. // -------------------------------------------------------------------------------
  188. //  function - GetArrayElem,  gets the ith element of the given FVF array
  189. //
  190. inline PBYTE 
  191. DXCrackFVF::GetArrayElem(PVOID pvPoints, DWORD iElem)
  192.     return ((PBYTE)pvPoints + (iElem * m_cBytesPerVertex)); 
  193. }
  194.  
  195. // -------------------------------------------------------------------------------
  196. //  function - SetPosition, sets the given position into the given FVF point
  197. //
  198. inline void 
  199. DXCrackFVF::SetPosition    (PVOID pvPoint, D3DXVECTOR3 *pvPos)
  200. {
  201.     memcpy(pvPoint, pvPos, sizeof(D3DXVECTOR3));
  202. }
  203.  
  204. // -------------------------------------------------------------------------------
  205. //  function - SetNormal, sets the given normal into the given FVF point
  206. //                  if no normal, this call is a no-op
  207. //
  208. inline void 
  209. DXCrackFVF::SetNormal      (PVOID pvPoint, D3DXVECTOR3 *pvNormal)
  210. {
  211.     if (m_oNormal)
  212.     {
  213.         memcpy(((PBYTE)pvPoint) + m_oNormal, pvNormal, sizeof(D3DXVECTOR3));
  214.     }
  215. }
  216.  
  217. // -------------------------------------------------------------------------------
  218. //  function - SetPointsize, sets the given point size into the given FVF point
  219. //                  if no normal, this call is a no-op
  220. //
  221. inline void 
  222. DXCrackFVF::SetPointSize   (PVOID pvPoint, float fPointSize)
  223. {
  224.     if (m_oPointSize)
  225.     {
  226.         memcpy(((PBYTE)pvPoint) + m_oPointSize, &fPointSize, sizeof(float));
  227.     }
  228. }
  229.  
  230. // -------------------------------------------------------------------------------
  231. //  function - SetDiffuse, sets the given diffuse color into the given FVF point
  232. //                  if no diffuse color, this call is a no-op
  233. //
  234. inline void 
  235. DXCrackFVF::SetDiffuse     (PVOID pvPoint, D3DCOLOR color)
  236. {
  237.     if (m_oDiffuse)
  238.     {
  239.         memcpy(((PBYTE)pvPoint) + m_oDiffuse, &color, sizeof(D3DCOLOR));
  240.     }
  241. }
  242.  
  243. // -------------------------------------------------------------------------------
  244. //  function - SetSpecular, sets the given specular color into the given FVF point
  245. //                  if no specular color, this call is a no-op
  246. //
  247. inline void 
  248. DXCrackFVF::SetSpecular    (PVOID pvPoint, D3DCOLOR color)
  249. {
  250.     if (m_oSpecular)
  251.     {
  252.         memcpy(((PBYTE)pvPoint) + m_oSpecular, &color, sizeof(D3DCOLOR));
  253.     }
  254. }
  255.  
  256. // -------------------------------------------------------------------------------
  257. //  function - SetTex1, sets the given Tex1 UV coords into the given FVF point
  258. //                  if no Tex1 coords, this call is a no-op
  259. //
  260. inline void 
  261. DXCrackFVF::SetTex1        (PVOID pvPoint, D3DXVECTOR2 *puvTex1)
  262. {
  263.     if (m_oTex[0])
  264.     {
  265.         memcpy(((PBYTE)pvPoint) + m_oTex[0], puvTex1, sizeof(D3DXVECTOR2));
  266.     }
  267. }
  268.  
  269.  
  270. //above function is skecthy, won't work for variable size textore coordinates, replaced
  271. //with following family
  272. inline void 
  273. DXCrackFVF::SetTexCoord    (PVOID pvPoint, UINT iTexCoord, D3DXVECTOR2 *pvTexCoord)
  274. {
  275.     if (iTexCoord < m_cTexCoords)
  276.     {
  277.         memcpy(((PBYTE)pvPoint) + m_oTex[iTexCoord], pvTexCoord, sizeof(D3DXVECTOR2));
  278.     }
  279. }
  280.  
  281. inline void 
  282. DXCrackFVF::SetTexCoord    (PVOID pvPoint, UINT iTexCoord, D3DXVECTOR3 *pvTexCoord)
  283. {
  284.     if (iTexCoord < m_cTexCoords)
  285.     {
  286.         memcpy(((PBYTE)pvPoint) + m_oTex[iTexCoord], pvTexCoord, sizeof(D3DXVECTOR3));
  287.     }
  288. }
  289.  
  290. inline void 
  291. DXCrackFVF::SetTexCoord    (PVOID pvPoint, UINT iTexCoord, D3DXVECTOR4 *pvTexCoord)
  292. {
  293.     if (iTexCoord < m_cTexCoords)
  294.     {
  295.         memcpy(((PBYTE)pvPoint) + m_oTex[iTexCoord], pvTexCoord, sizeof(D3DXVECTOR4));
  296.     }
  297. }
  298.  
  299. inline void 
  300. DXCrackFVF::SetTexCoord(PVOID pvPoint, UINT iTexCoord, float *pvTexCoord)
  301. {
  302.     if (iTexCoord < m_cTexCoords)
  303.     {
  304.         memcpy(((PBYTE)pvPoint) + m_oTex[iTexCoord], pvTexCoord, sizeof(float));
  305.     }
  306. }
  307.  
  308.  
  309.  
  310.  
  311. // -------------------------------------------------------------------------------
  312.  
  313. //  function - SetWeight, sets the given blend weight into the given FVF point
  314. //                  if the specified blend weight is not present, just return
  315. //
  316. inline void 
  317. DXCrackFVF::SetWeight     (PVOID pvPoint, UINT iWeight, float fWeight)
  318. {
  319.     if (iWeight < m_cWeights)
  320.     {
  321.         memcpy(((PBYTE)pvPoint) + sizeof(D3DXVECTOR3) + sizeof(float) * iWeight, &fWeight, sizeof(float));
  322.     }
  323. }
  324.  
  325. // -------------------------------------------------------------------------------
  326.  
  327. //  function - SetWeight, sets the given blend weight into the given FVF point
  328. //                  if the specified blend weight is not present, just return
  329. //
  330. inline float
  331. DXCrackFVF::FGetWeight     (PVOID pvPoint, UINT iWeight)
  332. {
  333.     return *(float*)(((PBYTE)pvPoint) + sizeof(D3DXVECTOR3) + sizeof(float) * iWeight);
  334. }
  335.  
  336. // -------------------------------------------------------------------------------
  337. //  function - SetIndices, sets the given indices into the given FVF point
  338. //                  if no indices, this call is a no-op
  339. //
  340. inline void
  341. DXCrackFVF::SetIndices    (PVOID pvPoint, DWORD dwIndices)
  342. {
  343.     if (BIndexedWeights())
  344.     {
  345.         *((LPDWORD)(((PBYTE)pvPoint) + sizeof(D3DXVECTOR3) + sizeof(float) * (CWeights() - 1))) = dwIndices;
  346.     }
  347. }
  348.  
  349. // -------------------------------------------------------------------------------
  350. //  function - PvGetPosition, returns a pointer to the position in the given FVF vertex
  351. //
  352. inline D3DXVECTOR3  *
  353. DXCrackFVF::PvGetPosition    (PVOID pvPoint)
  354. {
  355.     return (D3DXVECTOR3*)pvPoint;
  356. }
  357.  
  358. // -------------------------------------------------------------------------------
  359. //  function - PvGetNormal, returns a pointer to the normal in the given FVF vertex
  360. //                              NOTE: the normal HAS to be present in the FVF format
  361. //
  362. inline D3DXVECTOR3 *
  363. DXCrackFVF::PvGetNormal      (PVOID pvPoint)
  364. {
  365.     return (D3DXVECTOR3*)  (((PBYTE)pvPoint) + m_oNormal);
  366. }
  367.  
  368. // -------------------------------------------------------------------------------
  369. //  function - FGetPointSize, returns a the pointsize in the given FVF vertex
  370. //                              NOTE: the point size HAS to be present in the FVF format
  371. //
  372. inline float
  373. DXCrackFVF::FGetPointSize    (PVOID pvPoint)
  374. {
  375.     return *( (float*)  (((PBYTE)pvPoint) + m_oPointSize) );
  376. }
  377.  
  378. // -------------------------------------------------------------------------------
  379. //  function - ColorGetDiffuse, returns the diffuse color in the given FVF vertex
  380. //                              NOTE: the color HAS to be present in the FVF format
  381. //
  382. inline D3DCOLOR  
  383. DXCrackFVF::ColorGetDiffuse  (PVOID pvPoint)
  384. {
  385.     return *(D3DCOLOR*)  (((PBYTE)pvPoint) + m_oDiffuse);
  386. }
  387.  
  388. // -------------------------------------------------------------------------------
  389. //  function - ColorGetSpecular, returns the specular color in the given FVF vertex
  390. //                              NOTE: the color HAS to be present in the FVF format
  391. //
  392. inline D3DCOLOR  
  393. DXCrackFVF::ColorGetSpecular (PVOID pvPoint)
  394. {
  395.     return *(D3DCOLOR*)  (((PBYTE)pvPoint) + m_oSpecular);
  396. }
  397.  
  398. // -------------------------------------------------------------------------------
  399. //  function - PuvGetTex1, returns a pointer to the tex1 position in the given FVF vertex
  400. //                              NOTE: the tex1 HAS to be present in the FVF format
  401. //
  402. inline D3DXVECTOR2  *
  403. DXCrackFVF::PuvGetTex1       (PVOID pvPoint)
  404. {
  405.     return (D3DXVECTOR2*)  (((PBYTE)pvPoint) + m_oTex[0]);
  406. }
  407.  
  408.  
  409.  
  410. //family of functions that cast the texture stage to a D3DXVector
  411. //of the right size
  412.  
  413. inline float*
  414. DXCrackFVF::PxGetTexCoord(PVOID pvPoint,UINT index)
  415. {
  416.     return (float*) (((PBYTE)pvPoint) + m_oTex[index]);
  417. }
  418.  
  419. inline D3DXVECTOR2  *
  420. DXCrackFVF::PxyGetTexCoord(PVOID pvPoint,UINT index)
  421. {
  422.     return (D3DXVECTOR2*) (((PBYTE)pvPoint) + m_oTex[index]);
  423. }
  424.  
  425.  
  426. inline D3DXVECTOR3  *
  427. DXCrackFVF::PxyzGetTexCoord(PVOID pvPoint,UINT index)
  428. {
  429.     return (D3DXVECTOR3*) (((PBYTE)pvPoint) + m_oTex[index]);
  430. }
  431.  
  432.  
  433. inline D3DXVECTOR4  *
  434. DXCrackFVF::PxyzwGetTexCoord(PVOID pvPoint,UINT index)
  435. {
  436.     return (D3DXVECTOR4*) (((PBYTE)pvPoint) + m_oTex[index]);
  437. }
  438. // -------------------------------------------------------------------------------
  439. //  function - PuvGetWeights, returns a pointer to the first weight 
  440. //                              NOTE: the weights HAS to be present in the FVF format
  441. //
  442. inline float  *
  443. DXCrackFVF::PfGetWeights       (PVOID pvPoint)
  444. {
  445.     return (float*)(((PBYTE)pvPoint) + sizeof(D3DXVECTOR3));
  446. }
  447.  
  448. // -------------------------------------------------------------------------------
  449. //  function - CbTexCoordSize, returns the size of the given set of texture coordinates
  450. //
  451. inline DWORD
  452. DXCrackFVF::CbTexCoordSize       (DWORD iTexCoord)
  453. {
  454.     if (iTexCoord < m_cTexCoords)
  455.         return x_rgdwTexCoordSize[(m_dwFVF >> (16 + iTexCoord * 2)) & 0x3];
  456.     else
  457.         return 0;
  458. }
  459.  
  460. // -------------------------------------------------------------------------------
  461. //  function - CbTexCoordSize, returns the sizes of all 8 texture coords (0 if not present)
  462. //
  463. inline void
  464. DXCrackFVF::GetTexCoordSizes       (DWORD *rgdwTexCoordSizes)
  465. {
  466.     DWORD iTexCoord;
  467.  
  468.     for (iTexCoord = 0; iTexCoord < m_cTexCoords; iTexCoord++)
  469.     {
  470.        
  471.         rgdwTexCoordSizes[iTexCoord] = x_rgdwTexCoordSize[(m_dwFVF >> (16 + iTexCoord * 2)) & 0x3];
  472.  
  473.  
  474.     }
  475.  
  476.     // set the rest to 0
  477.     for (;iTexCoord < 8; iTexCoord++)
  478.     {
  479.         rgdwTexCoordSizes[iTexCoord] = 0;
  480.     }
  481. }
  482.  
  483.  
  484.     inline void         GetTexCoordSizes(DWORD *rgdwTexCoordSizes);
  485.  
  486. // -------------------------------------------------------------------------------
  487. //  function - DwGetIndices, returns the indices in the given FVF vertex
  488. //                              NOTE: the indices HAS to be present in the FVF format
  489. //
  490. inline DWORD
  491. DXCrackFVF::DwGetIndices    (PVOID pvPoint)
  492. {
  493.     return *( (DWORD*)  (((PBYTE)pvPoint) + sizeof(D3DXVECTOR3) + sizeof(float) * (CWeights() - 1)) );
  494. }
  495.  
  496. #endif //__DXCRACKFVF_H__
  497.